package no.testspill;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public class LineSegment {
	
	private float startX, startY, endX, endY;
	float slope;
	public final float COLLISION_THRESHOLD = 8.0f;
	public ArrayList<float[]> coordinates;
	public float[] collisionPoint;
	
	public LineSegment(float sx, float sy,  float ex,float ey){
		init(sx,sy,ex,ey);
		setSlope();
		collisionPoint = new float[2];
		fillCoordinates();
	}
	
	public void init(float sx, float sy,  float ex,float ey){
		if(ex < sx){
			startX = ex;
			startY = ey;
			endX = sx;
			endY = sy;
		}
		else{
			startX = sx;
			startY = sy;
			endX = ex;
			endY = ey;
		}
			
	} 
	
	public float getStartX() {
		return startX;
	}

	public void setStartX(float startX) {
		this.startX = startX;
	}

	public float getStartY() {
		return startY;
	}

	public void setStartY(float startY) {
		this.startY = startY;
	}

	public float getEndX() {
		return endX;
	}

	public void setEndX(float endX) {
		this.endX = endX;
	}

	public float getEndY() {
		return endY;
	}

	public void setEndY(float endY) {
		this.endY = endY;
	}

	public float getSlope() {
		return slope;
	}
	
	public float getCollisionPointX(){
		return collisionPoint[0];
	}
	
	public float getCollisionPointY(){
		return collisionPoint[1];
	}

	public void setSlope(float slope) {
		this.slope = slope;
	}

	public float getCOLLISION_THRESHOLD() {
		return COLLISION_THRESHOLD;
	}

	public void setSlope(){
		float dx = endX - startX;
		float dy = endY - startY;
		slope = (float)dy/dx;
	}
	
	public float getYByX(float x){
		return (slope*x)+startY;
	}
	
	public void fillCoordinates(){
		coordinates = new ArrayList<float[]>();
		int xInterval = Math.abs((int) startX - (int) endX);
		for (int i = 0; i <= xInterval; i++) {
			float[] coo = {startX+i,getYByX(i)};
			coordinates.add(coo);
		}
	}
	
	public void printCoordinates(){
		for (int i = 0; i < coordinates.size(); i++) {
			System.out.println(coordinates.get(i)[0] + ","+coordinates.get(i)[1]);
		}
	}
	
	public void drawTestLine(Canvas c){
		Paint paint = new Paint();
		paint.setColor(Color.BLACK);
		paint.setStrokeWidth(12);
		paint.setStyle(Paint.Style.STROKE);
		for (int i = 0; i < coordinates.size(); i++) {
			c.drawPoint(coordinates.get(i)[0] , coordinates.get(i)[1] , paint);
		} 
	}
	
	public boolean checkCollision(float x, float y){
		int xInterval = Math.abs((int) startX - (int) endX);
		for (float i = 0; i <= xInterval; i+=1.0) {
			float diffX = Math.abs((startX+i)-(x));
			float diffY = Math.abs(getYByX(i)-y);
			if(diffX < COLLISION_THRESHOLD && diffY < COLLISION_THRESHOLD){
				collisionPoint[0] = x;
				collisionPoint[1] = y;
				System.out.println(" KOLLLLI FUCKINGS FITTE-SJOOOOOOOOOOOOOOON");
				System.out.println(diffX);
				System.out.println(diffY);
				return true;
			} 
		}
		 
		return false;
	}
	
	
	
}
